﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LERP.Domain;
using LERP.Domain.Enums;
using LERP.Web.Controllers.ViewModels.BugViewModels;
using LERP.Web.Controllers.ViewModels.ContractViewModels;
using Sharp.Framework;
using Sharp.Framework.Queries;
using SharpArch.NHibernate.Web.Mvc;

namespace LERP.Web.Controllers
{
    public class BugController : BaseController
    {
        
        private readonly IQuery _query;

        public BugController(IQuery query)
        {
            this._query = query;
        }
        /// <summary>
        /// 获取列表
        /// </summary> 
        /// <param name="status"></param>
        /// <param name="keyword"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index(int? status,string keyword, int page = 1, int size = 12)
        { 
            var query = _query.FindAll<Bug>();

            if (status.HasValue)
            {
                query = query.Where(m => m.Status==status.Value);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(m => m.Title.Contains(keyword));
            }
            var model = query.OrderByDescending(m => m.Modified).ToPagedList(page, size);

            return View(model);
        }

        [Transaction]
        [HttpGet]
        public ActionResult Status(int id,int status, string returnUrl)
        {
            var entity = _query.Get<Bug>(id);

            if (null == entity)
            {
                return View("NoFound");
            }
            entity.Remark += string.Format("<分隔符>用户{0}[{1}]将问题从{2}状态修改为{3}状态----{0}--{4}", Current.Name, Current.UserName, FormatStatusString(entity.Status), FormatStatusString(status), DateTime.Now);
            entity.Status = status;
            _query.Save(entity);
            if (string.IsNullOrEmpty(returnUrl))
            {
                return RedirectToAction("Index");
            }
            return Redirect(returnUrl);
        }

        private string FormatStatusString(int status)
        {
            switch (status)
            {
                case 0:
                    return "未处理";
                case 1:
                    return "已解决";
                case 2:
                    return "保留现状";
                case 3:
                    return "关闭";
            }
            return status.ToString(CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// 添加备注
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Remark(int id,  string returnUrl)
        {
            var entity = _query.Get<Bug>(id); 
            if (null == entity)
            {
                return View("NoFound");
            }
            return View(new BugRemarkViewModel
            {
                Name = entity.Name,
                Content = entity.Content,
                Remarks =entity.Remark,
                Title = entity.Title
            });
        }
        /// <summary>
        /// 添加备注 提交
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        [HttpPost]
        [Transaction]
        public ActionResult Remark(int id, BugRemarkViewModel model, string returnUrl)
        {
            try
            { 
                if (ModelState.IsValid)
                {

                    var entity = _query.Get<Bug>(id);
                    if (null == entity)
                    {
                        return View("NoFound");
                    }
                    entity.Modified = DateTime.Now; 
                    entity.Remark += "<分隔符>"+model.Remark+"----"+Current.Name+"--"+DateTime.Now;
                    _query.Save(entity);
                    if (string.IsNullOrEmpty(returnUrl))
                    {
                        return RedirectToAction("Index");
                    }
                    return Redirect(returnUrl);

                }
            }
            catch
            {
                ModelState.AddModelError("", "系统繁忙,请稍后再试");
            }
            return View();
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Create()
        { 
            return View(new BugViewModel
            {
                Name = Current.Name
            });
        }
         

        /// <summary>
        /// 提交 新增合同
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Transaction]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(BugViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var entity = new Bug
                    { 
                        Created = DateTime.Now,
                        Modified = DateTime.Now,
                        Name = model.Name,
                        Content = model.Content,
                        Title = model.Title
                    };
                   
                    _query.Save(entity);
                    return View("Thinkyou");
                }
            }
            catch
            {
                ModelState.AddModelError("", "系统繁忙,请稍后再试");
            } 
            return View(model);
        }
    }
}
